load("js/luaclient.js")
load("js/v6/citic/citic_credit_lua.js")
load("js/v6/citic/citic_credit_login.js")
load("js/v6/citic/citic_credit_crawlhandle.js")

var v6_citic_credit_java_funcs = {

    openHomePage : function(){
        logger.info("CITCICREDIT taskkey={} 检查当前代理是否可用 {}",context.get(LUA_TASK_KEY),context.get(LUA_TASK_PROXY_INFO));
        var	html = get("https://creditcard.ecitic.com/citiccard/ucweb/entry.do");
        storeDataToFile(context,html, "loginHtml.html");
    },

    checkControlAvailable:function(){ return true },
    checkTargetWebsiteAvailable: function(){ return true },
    //检查代理是否可用
    checkProxyAvailable: function () {


        var isCorrectFunc = function(){
            return is_exist_by_id("phoneNbr")
        }
        var getErrorMsgFunc = function() {
            return "代理ip异常";
        }
        return assertTrueOrThrowException("", isCorrectFunc, getErrorMsgFunc)

    },
    needRefreshHomePageOrNot : function() {
        return equals(context.get("currentErrorCode"), BALO_99602_50)
    },
    needResetEnvironment:function(){
        return equals(context.get("currentErrorCode"),BALO_99602_40);
    },
    login:function () {
        var mobile = getLoginParams(context)[0];
        set_input_text_by_id("phoneNbr", mobile);
        inputImg();
        inputSms();
        //当前链接
        var sms_currentUrl=get_url();
        logger.info("sms_currentUrl="+sms_currentUrl);
        //https://creditcard.ecitic.com/citiccard/ucweb/toSetMm.do 注册链接
        if(sms_currentUrl.contains("toSetMm.do")){
            throwBankExceptionByErrorMsg("您还未绑卡，暂不支持业务办理");
        }
        //https://creditcard.ecitic.com/citiccard/ucweb/entry.do  首页
        //点击下一步,仍然停留在首页,说明当前流程中断,返回错误信息
        if(sms_currentUrl.contains("entry.do")){
            var screenShots = luaFuncs.getScreenShots();
            logger.info("下一步截图路径{}", screenShots);
            //点击下一步页面,出现错误
            var nextErrorMsg=luaFuncs.getInnerHTML("reg_phone_error");
            //有错误信息，才会包含<i></i>，没有错误信息，由于网络原因，可能返回return is null
            if(nextErrorMsg.contains("<i></i>")) {
                throwBankExceptionByErrorMsg(nextErrorMsg.replace("<i></i>", ""));
            }
        }
        Thread.sleep(1000);
        var pWord = context.get(Constants.LUA_TASK_TOKEN);
        var inputPassWordByIdResult=inputPassWordById(pWord);
        logger.info("presult="+inputPassWordByIdResult);
        clickLoginButton("login");
        Thread.sleep(2000);
        var login_currentUrl=get_url();
        logger.info("login_currentUrl_1="+login_currentUrl);
        //https://creditcard.ecitic.com/citiccard/ucweb/memberset.do 重置
        //登录信息校验
        var loginMsg=luaFuncs.getInnerHTML("mm_emsg");
        logger.info("loginMsg="+loginMsg);
        var screenShots = luaFuncs.getScreenShots();
        logger.info("登录截图路径{}", screenShots);
        //登录时返回的错误信息
        if(loginMsg!=""){
            if(loginMsg.contains("<i></i>")){
                throwBankExceptionByErrorMsg(loginMsg.replace("<i></i>",""));
            }
        }
        // 获取首页源码 判断是否进入主页
        var indexPage = luaFuncs.zx_get_html();
        storeDataToFile(context,indexPage, "indexPage.html");
        context.put(keys.INDEX_PAGE,indexPage);
        var postdatam = new HashMap();
        //获取卡列表页面
        var cardHtml = ajax_post("https://creditcard.ecitic.com/citiccard/newonline/common.do?func=querySignCards", postdatam);
        Thread.sleep(3000);
        storeDataToFile(context,cardHtml, "cardList.html");
        var smsCheckResult = {};
        if (cardHtml.indexOf("您还未绑卡")!=-1 || !(cardHtml.indexOf("cardlist")!=-1)) {
            smsCheckResult.errorMsg = "您还未绑卡，暂不支持业务办理"
            smsCheckResult.statuts = "EMPTYCARD";
        }else{
            context.put(keys.CARD_INFO, cardHtml);
            var doc = Jsoup.parse(cardHtml);
            var cardList = doc.select("cardlist");
            //卡数量
            context.put(keys.CRED_CARD_SIZE, cardList.size());
            //卡列表
            context.put(keys.CARD_LIST, cardList);
            smsCheckResult.statuts = "OK";
        }
        var checkCardListFunc = function (smsCheckResult) {
            return equals(smsCheckResult.statuts, "OK");
        }
        var getCheckCardListError = function (smsCheckResult) {
            return smsCheckResult.errorMsg;
        }
        return assertTrueOrThrowException(smsCheckResult, checkCardListFunc, getCheckCardListError)
    },
    //抓取过程
    crawl :function(){
        //中信信用卡抓取解析主流程
        credit_crawl_main();

        var list = new ArrayList();
        //从上下文，获取数据对象集合
        var data_list = context.get(ALL_DATA);
        for(var i=0;i<data_list.size( );i++){
            //添加每个卡对象
            list.add(data_list.get(i));
        }
        context.put("credit_data", list) ;
    },

    exitLogin :function () {
    }
}




